package com.sinodata.bsm.center.service.resource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.cache.MonitorTypeCache;
import com.sinodata.bsm.center.cache.ResViewCache;
import com.sinodata.bsm.center.cache.ResViewRelCache;
import com.sinodata.bsm.center.dao.impl.ResViewDaoImpl;
import com.sinodata.bsm.common.exception.AppException;
import com.sinodata.bsm.common.vo.MonitorType;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResView;
import com.sinodata.bsm.common.vo.ResViewRel;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author dongyushi
 * @version 1.0
 * 
 * <p>
 * History:
 * 
 * Date Author Version Description
 * ---------------------------------------------------------------------------------
 * 2012-5-21 AM 11:29:51 dongyushi 1.0 To create
 * </p>
 * 
 * @since
 * @see
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class ResViewService {

    @Autowired
    private ResViewCache resViewCache;
    @Autowired
    private ResViewRelCache resViewRelCache;

    @Autowired
    private ResViewDaoImpl resViewDao;
    @Autowired
    private MonitorTypeCache monitorTypeCache;

    /**
     * 通过ID查询
     * 
     * @param id
     * @return
     */
    public ResView get(Long id) {
        return resViewCache.get(id);
    }

    /**
     * 分页查询
     * 
     * @param page
     * @param condition
     * @return
     */
    public Page<ResView> findByPage(Page<ResView> page) {
        return resViewDao.findByPage(page);
    }

    /**
     * 新增
     * 
     * @param resView
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(ResView resView) {
        resViewDao.save(resView);
    }

    /**
     * 修改
     * 
     * @param resView
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(ResView resView) {
        resViewDao.update(resView);
    }

    /**
     * 删除
     * 
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public ResView delete(Long[] ids) throws AppException {
        for (Long id : ids) {
            if (id == 1) {
                throw new AppException("业务规则约束，默认视图不能被删除。");
            }
        }
        resViewDao.batchDelete(ids);
        return null;

    }

    public ResView[] findAll() {
        ResView[] views = resViewCache.findAll();
        Arrays.sort(views);
        return views;
    }

    public int getResViewNum() {
        List<ResView> list = (List<ResView>) resViewDao.findAll(new ResView().getClass());
        return list.size();
    }

    public List<ResViewRel> getResViewRelByViewId(Long viewId) {
        return resViewRelCache.getResViewRelByViewId(viewId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteResViewRel(Long resId) {
        List<ResViewRel> relList = resViewRelCache.getResViewRelByResId(resId);
        if (relList.size() > 0) {
            for (int i = relList.size() - 1; i >= 0; i--) {
                deleteResViewRel(relList.get(i));
            }
        }
    }

    public List<ResViewRel> getResViewRelByResId(Long resId) {
        return resViewRelCache.getResViewRelByResId(resId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveResViewRel(ResViewRel resViewRel) {
        resViewDao.save(resViewRel);
        resViewRelCache.put(resViewRel.getResViewId(), resViewRel);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void batchSaveResViewRel(List<Res> resList, Long viewId) throws Exception {
        List<ResViewRel> resViewRelList = new ArrayList<ResViewRel>();
        for (Res res : resList) {
            ResViewRel viewRel = new ResViewRel();
            viewRel.setResId(res.getId());
            viewRel.setResViewId(viewId);
            resViewRelList.add(viewRel);
        }
        resViewDao.batchSave(resViewRelList);
        for (ResViewRel resViewRel : resViewRelList) {
            resViewRelCache.put(resViewRel.getResViewId(), resViewRel);
        }

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteResViewRel(ResViewRel resViewRel) {
        resViewRelCache.remove(resViewRel.getResId(), resViewRel.getResViewId());
        resViewDao.delete(resViewRel);

    }

    public List<Long> findChildByViewId(Long viewId, List<Long> privilegeRess) {
        return resViewDao.findChildByViewId(viewId, privilegeRess);
    }

    public List<MonitorType> findAllMonitorType() {
        return monitorTypeCache.findAll();
    }

    public Long findMotiResType(Long resTypeId) {
        return resViewDao.findMotiResType(resTypeId);
    }
}
